package com.yuesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.R;
import com.yuesystem.entity.ChangeItem1;
import com.yuesystem.entity.Question;
import com.yuesystem.service.ChangeItem1Service;
import com.yuesystem.service.QuestionService;
import java.util.List;

import com.yuesystem.service.impl.FeedbackServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 常见问题控制类.
 */
@RestController
@RequestMapping("/question")
@CrossOrigin
@Slf4j
public class QuestionController {
  @Autowired
  QuestionService questionService;
  
  @Autowired
  ChangeItem1Service changeItem1Service;
  
  /**
   * 获取提问次数最多的问题.
   *
   * @return 获取提问次数最多的五个问题
   */
  @GetMapping("/get")
  public R<List<Question>> getQuestion() {
    List<Question> list = questionService.getQuestion();
    return R.success(list);
  }
  
  /**
   * 获取问题答案.
   *
   * @param id 问题的id
   * @return 问题主体
   */
  @GetMapping("/getAnswer")
  public R<Question> getAnwser(@RequestParam Long id) {
    Question question = questionService.getAnswer(id);
    return R.success(question);
  }
  
  /**
   * 根据关键词得到问题.
   *
   * @param keyWord 关键词
   * @param page 页面个数
   * @param pageSize 页面大小
   * @return 问题列表主体
   */
  @GetMapping("/select")
  public R<Page<Question>> selectQuestion(@RequestParam String keyWord, @RequestParam int page,
                                          @RequestParam int pageSize) {
    Page<Question> pageInfo = new Page<>(page, pageSize);
    LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(Question::getQuestion, keyWord);
    questionService.page(pageInfo, queryWrapper);
    return R.success(pageInfo);
  }
  
  /**
   * 根据问题得到答案，并且增加该问题的访问次数.
   *
   * @param question 问题主体
   * @return 问题答案
   */
  @GetMapping("/ans")
  public String ansQuestion(@RequestBody Question question) {
    question.setNum(question.getNum() + 1);
    return question.getReply();
  }
  
  /**
   * 添加一个问题.
   *
   * @param question 问题
   * @param reply 答案
   * @return 添加操作反馈
   */
  @RequestMapping("/add")
  public R<Boolean> addQuestion(@RequestParam String question, @RequestParam String reply) {
    if (question.isEmpty() || reply.isEmpty()) {
      return R.success(false);
    }
    Question question0 = new Question();
    question0.setQuestion(question);
    question0.setReply(reply);
    question0.setNum(0);
    ChangeItem1 changeItem1=new ChangeItem1();
    changeItem1.setUserId(FeedbackServiceImpl.userId);
    changeItem1.setOperate("增加");
    changeItem1.setSubjectNew(question);
    changeItem1.setRelationNew("答案");
    changeItem1.setObjectNew(reply);
    changeItem1Service.save(changeItem1);
    Boolean flag = questionService.save(question0);
    return R.success(flag);
  }
  
  /**
   * 更新问题.
   *
   * @param question 问题
   * @param reply 答案
   * @param id 问题id
   * @return 更新操作反馈
   */
  @RequestMapping("/update")
  public R<Boolean> updateQuestion(@RequestParam String question,
                                   @RequestParam String reply, @RequestParam Long id) {
    if (question.isEmpty() || reply.isEmpty()) {
      return R.success(false);
    }
    Question question0 = questionService.getById(id);
    String oldReply = question0.getReply();
    question0.setQuestion(question);
    question0.setReply(reply);
    Boolean flag = questionService.updateById(question0);
    ChangeItem1 changeItem1=new ChangeItem1();
    changeItem1.setUserId(FeedbackServiceImpl.userId);
    changeItem1.setOperate("修改");
    changeItem1.setSubjectOld(question);
    changeItem1.setRelationOld("答案");
    changeItem1.setObjectOld(oldReply);
    changeItem1.setSubjectNew(question);
    changeItem1.setRelationNew("答案");
    changeItem1.setObjectNew(reply);
    changeItem1Service.save(changeItem1);
    return R.success(flag);
  }
  
  @RequestMapping("/delete")
  public R<Boolean> deleteQuestion(@RequestParam Long id) {
    Question question=questionService.getById(id);
    ChangeItem1 changeItem1=new ChangeItem1();
    changeItem1.setUserId(FeedbackServiceImpl.userId);
    changeItem1.setOperate("删除");
    changeItem1.setSubjectOld(question.getQuestion());
    changeItem1.setRelationOld("答案");
    changeItem1.setObjectOld(question.getReply());
    changeItem1Service.save(changeItem1);
    Boolean flag = questionService.removeById(id);
    return R.success(flag);
  }
}
